﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ThrowModal
{
    public partial class MainPage : UserControl
    {
        #region Variables
        bool AreDetailsShown = true; // show details by default
        #endregion

        #region Methods

        [ScriptableMember()]
        public void HandleResult(string result)
        {
            // update the results - this method will get called from the JavaScript Callback function
            this.tbResults.Text = result;
        }

        public MainPage()
        {
            // Required to initialize variables
            InitializeComponent();

            // register the HandleResult method so it can be called from JavaScript
            HtmlPage.RegisterScriptableObject("ThrowModalSL", this);

            // set inital values and display options
            this.sliderWidth.Value = 800;
            this.sliderHeight.Value = 600;
            this.cbShowClose.IsChecked = true;
            this.cbAllowMaximize.IsChecked = true;
            this.cbShowMaximized.IsChecked = false;
            this.cbAutoSize.IsChecked = true;
            if (AreDetailsShown)
            {
                this.showDetails.Begin();
            }
            else
            {
                this.HideDetails.Begin();
            }
        }

        #endregion

        #region Events

        #region Slider Values Changes
        private void sliderWidth_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                this.tbWidth.Text = ((int)this.sliderWidth.Value).ToString();
            }
            catch { }
        }

        private void sliderHeight_ValueChanged(object sender, System.Windows.RoutedPropertyChangedEventArgs<double> e)
        {
            try
            {
                this.tbHeight.Text = ((int)this.sliderHeight.Value).ToString();
            }
            catch { }
        }

        private void tbWidth_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // determine if the return key was pressed
            if (e.Key == Key.Enter)
            {
                try
                {
                    this.sliderWidth.Value = Convert.ToInt32(tbWidth.Text);
                }
                catch { }
            }
        }

        private void tbHeight_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            // determine if the return key was pressed
            if (e.Key == Key.Enter)
            {
                try
                {
                    this.sliderHeight.Value = Convert.ToInt32(tbHeight.Text);
                }
                catch { }
            }
        }
        #endregion

        #region Checkbox Changes
        private void cbAutoSize_Checked(object sender, System.Windows.RoutedEventArgs e)
        {
            // if autosize has been selected, disable the size sliders
            this.sliderHeight.IsEnabled = false;
            this.sliderWidth.IsEnabled = false;
            this.tbWidth.IsReadOnly = true;
            this.tbHeight.IsReadOnly = true;
        }

        private void cbAutoSize_Unchecked(object sender, System.Windows.RoutedEventArgs e)
        {
            // if autosize has been deselected, enable the size sliders
            this.sliderHeight.IsEnabled = true;
            this.sliderWidth.IsEnabled = true;
            this.tbWidth.IsReadOnly = false;
            this.tbHeight.IsReadOnly = false;
        }
        #endregion

        #region Show / Hide details
        private void butDetails_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            AreDetailsShown = !AreDetailsShown;

            if (AreDetailsShown)
            {
                // show the details canvas
                this.HideDetails.Stop();
                this.showDetails.Begin();
            }
            else
            {
                // hide the details canvas
                this.showDetails.Stop();
                this.HideDetails.Begin();
            }
        }
        #endregion

        private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // clear the results
            tbResults.Text = "";

            // handle our x and y co-ords
            int? x;
            int? y;
            try
            {
                x = Convert.ToInt32(this.tbX.Text);
                y = Convert.ToInt32(this.tbY.Text);
            }
            catch
            {
                // if either x or y could not be converted to an integer we need to pass both as null
                x = null;
                y = null;
            }


            // the name of the JavaScript function to call that will open the modal
            string functionName = "ThrowModal";

            // these functionName suffixes are used to call the appropriate
            // JavaScript function based on the options selected
            if (cbAutoSize.IsChecked == true)
                functionName += "AutoSize";
            if (x != null && y != null)
                functionName += "XY";

            // Invoke the JavaScript function
            HtmlPage.Window.Invoke(
                functionName,
                string.IsNullOrEmpty(this.tbURL.Text) ? null : this.tbURL.Text, // URL
                this.tbHTML.Text, // HTML
                this.tbTitle.Text, // Title
                this.tbCallback.Text, // Callback
                this.tbArgs.Text, // Args (string)
                (int)this.sliderWidth.Value, // Width
                (int)this.sliderHeight.Value, // Height
                x, // X
                y, // X
                cbAutoSize.IsChecked, // AutoSize
                cbAllowMaximize.IsChecked, // AllowMaximize
                cbShowMaximized.IsChecked, // ShowMaximize
                cbShowClose.IsChecked); // ShowClose
        }

        private void tb_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                this.Button_Click(null, null);
        }

        #endregion
    }
}